package com.yuntsg.nnsfcp.test.test;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadData {

    public static List<HtmlEntity> main(String html) {
        List<HtmlEntity> ls = new ArrayList<>();
        Document doc = Jsoup.parse(html);
        Element first = doc.select("table.table").select(".table-bordered").select(".table-striped").first();
        Map<String, String> fenquMap = new HashMap<>();
        Element first1 = doc.select("head").first().select("style").first();
        String[] split = first1.html().replaceAll("\\s*", "").split("\\.");
        for (int i1 = 0; i1 < split.length; i1++) {
            if (!StringUtils.isEmpty(split[i1])) {
                String tenp = split[i1];
                String replace = tenp.replace(":before", "!@#");
                String[] split1 = replace.split("!@#");
                fenquMap.put(split1[0], split1[1]);
            }
        }
        if (first != null) {
            Elements trs = first.select("tr");
            for (int i = 1; i < trs.size(); i++) {
                Elements tds = trs.get(i).select("td");
                String leibie = "";
                String subject = "";
                String fenqu = "";
                boolean isTop = false;
                for (int j = 0; j < tds.size(); j++) {
                    Element td = tds.get(j);
                    if (j == 0) {
                        leibie = td.text();
                    }
                    if (j == 1) {
                        subject = td.text();
                    }
                    if (j == 2) {
                        String s = fenquMap.get(td.select("span").first().attributes().get("class"));
                        JSONObject jsonObject = JSONObject.parseObject(s);
                        String content = jsonObject.getString("content");
                        fenqu = content;
                    }
                    if (j == 3) {
                        isTop = td.text().equals("是");
                    }
                }
                HtmlEntity htmlEntity = new HtmlEntity();
                htmlEntity.setDalei(leibie);
                htmlEntity.setSub(subject);
                htmlEntity.setFenqu(fenqu);
                htmlEntity.setTop(isTop + "");
                ls.add(htmlEntity);
            }
        }
        return ls;
    }
}
